package com.zboot.zbootadmin.project.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zboot.zbootadmin.common.utils.PagingResult;
import com.zboot.zbootadmin.project.base.command.machinery.MachineryPartBrandListCommand;
import com.zboot.zbootadmin.project.base.entity.MachineryBrand;
import com.zboot.zbootadmin.project.base.entity.MachineryPartBrand;
import com.zboot.zbootadmin.project.base.mappers.MachineryPartBrandMapper;
import com.zboot.zbootadmin.project.base.service.MachineryPartBrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author draw
 * @since 2021-05-08
 */
@Service
public class MachineryPartBrandServiceImpl extends ServiceImpl<MachineryPartBrandMapper, MachineryPartBrand> implements MachineryPartBrandService {
    @Autowired
    private MachineryPartBrandMapper machineryPartBrandMapper;

    @Override
    public void updateToDelete(Long id) {
        machineryPartBrandMapper.updateToDelete(id);
    }

    @Override
    public PagingResult pageList(MachineryPartBrandListCommand command) {
        Page pageVo = new Page(command.getPage(), command.getPageSize());
        LambdaQueryWrapper<MachineryPartBrand> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(command.getName())) {
            queryWrapper.like(MachineryPartBrand::getName, command.getName());
        }

        queryWrapper.orderByDesc(MachineryPartBrand::getCreateDate);
        IPage<MachineryBrand> iPage = machineryPartBrandMapper.selectPage(pageVo, queryWrapper);
        PagingResult pagingResult = new PagingResult();
        pagingResult.setItems(iPage.getRecords());
        pagingResult.setTotalCount(iPage.getTotal());
        return pagingResult;
    }
}
